package com.wuyou.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuyou.domain.Result;
import com.wuyou.em.ResultEnum;
import com.wuyou.entity.House;
import com.wuyou.entity.HouseDetails;
import com.wuyou.service.IHouseService;
import com.wuyou.service.IHouseDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张钧程
 * @since 2022-09-29
 */
@RestController
@RequestMapping("house")
public class HouseController {

    @Autowired
    private IHouseService hs;

    @Autowired
    private IHouseDetailsService hds;

    //查询全部房源，如果hostId不为空则查询该房东名下的全部房源
    @GetMapping("getHouseAll")
    public Result getHouseAll(Long hostId){
        List<House> houses;
        if (hostId != null) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("host_id",hostId);
            houses = hs.list(wrapper);
            return Result.OK().setData(houses);
        }
        houses = hs.list();
        return Result.OK().setData(houses);
    }

    //查询全部未出租房源，如果hostId不为空则查询该房东名下的全部未出租房源
    @GetMapping("getHouseAllUnLeased")
    public Result getHouseAllUnLeased(Long hostId){
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        if (hostId != null) {
            wrapper.eq("host_id",hostId);
        }
        wrapper.eq("status", 0);
        List<House> h = hs.list(wrapper);
        if(h.isEmpty()){
            return Result.ERROR();
        }

        return Result.OK().setData(h);
    }

    //查询全部已出租房源，如果hostId不为空则查询该房东名下的全部已出租房源
    @GetMapping("getHouseAllLeased")
    public Result getHouseAllLeased(Long hostId){
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        if (hostId != null) {
            wrapper.eq("host_id",hostId);
        }
        wrapper.eq("status",1);
        List<House> h = hs.list(wrapper);
        if(h.isEmpty()){
            return Result.ERROR();
        }
        return Result.OK().setData(h);
    }

    //查询房源的详细信息，detailId不能为空
    @GetMapping("getLeasedDetail")
    public Result getLeasedDetail(Long detailId){
        HouseDetails byId = hds.getById(detailId);
        if (byId == null){
            return Result.ERROR();
        }
        return Result.OK().setData(byId);
    }

    //添加房源及房源详情信息
    @PostMapping("addhouse")
    public Result addHouse(House house,HouseDetails houseDetails){
        if (BeanUtil.isEmpty(house)){
            return Result.R(ResultEnum.MESSAGE_ERROR);
        }
        house.setStartTime(new Date());
        Long house_id = hs.saveReturnId(house);
        if (house_id == null){
            return Result.ERROR();
        }else{
            houseDetails.setHouseId(house_id);
            boolean save1 = hds.save(houseDetails);
            if (save1){
                return Result.OK();
            }else {
                return Result.ERROR();
            }
        }
    }

    //修改房源及房源详情信息
    @PutMapping
    public Result updateHouse(House house,HouseDetails houseDetails){
        if (BeanUtil.isEmpty(house)){
            return Result.R(ResultEnum.MESSAGE_ERROR);
        }
        boolean b = hs.updateById(house);
        if (b){
            QueryWrapper<HouseDetails> wrapper = new QueryWrapper<>();
            wrapper.eq("house_id",house.getHouseId());
            boolean update = hds.update(houseDetails, wrapper);
            if (update){
                return Result.OK();
            }else{
                return Result.ERROR();
            }
        }else{
            return Result.ERROR();
        }
    }

    //删除房源及房源详情信息
    @DeleteMapping
    public Result deleteHouse(Long houseId){
        if (houseId == null){
            return Result.R(ResultEnum.MESSAGE_ERROR);
        }
        boolean remove = hs.removeById(houseId);
        if (remove){
            QueryWrapper<HouseDetails> wrapper = new QueryWrapper<>();
            wrapper.eq("house_id",houseId);
            hds.remove(wrapper);
            return Result.OK();
        }else {
            return Result.ERROR();
        }
    }
}

